package fr.esgi.pages;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import fr.esgi.classes.Groupe;
import fr.esgi.classes.Personne;
import fr.esgi.classes.Projet;
import fr.esgi.donnees.Database;


/**
 * Servlet implementation class ValidationSoutenance
 */
public class ValidationSoutenance extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ValidationSoutenance() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String projet=request.getParameter("projet");
		String ordre=request.getParameter("ordre");
		String  heure=request.getParameter("heure");
		String  minute=request.getParameter("minute");
		String  heuref=request.getParameter("heuref");
		String  minutef=request.getParameter("minutef");
		String  duree=request.getParameter("duree");
		String date=request.getParameter("calendrier");
		ArrayList <Groupe>groupe = null;
		Projet p;
		Database c =new Database();

		try {
			p = c.buildProjet(projet);
			groupe=c.afficherEquipe(projet);

		} catch (ClassNotFoundException | SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}


		if (ordre.equals("automatique"))
			Collections.shuffle(groupe);



		// TODO Auto-generated catch block

		int n = 0 ;	



		response.setContentType("application/pdf"); 
		//PrintWriter out = response.getWriter();
		
		//recuperation des horaires de debut de soutenance
		int min = Integer.parseInt(minute);
		int h = Integer.parseInt(heure);

		//Soutenance soutenance=new Soutenance(Integer.parseInt(nom),
		//recuperation des horaires de fin de soutenance
		int minf = Integer.parseInt(minutef);
		int hf = Integer.parseInt(heuref);
			try {
				c.deleteSoutenance(projet);
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		Document document = new Document();

		try {
			PdfWriter.getInstance(document, 
					response.getOutputStream());

			document.open();
			document.addTitle("Planning Soutenance "+ projet);
			Paragraph preface = new Paragraph();
			//addEmptyLine(preface, 1);
			preface.add(new Paragraph("Planning Soutenance : " + projet ));
			document.add(preface);
			preface.add(new Paragraph("\n"));
			PdfPTable table = new PdfPTable(3);
			table.addCell("Equipe");
			table.addCell("Nom des Etudiants");
			table.addCell("Horaire");


			for (int i = 0; i <groupe.size(); i++) {

				table.addCell(""+groupe.get(i).getId());
				String etudiant =" ";
				for (int j = 0; j <groupe.get(i).getTabEtudiant().size(); j++) {
					etudiant=etudiant+groupe.get(i).getTabEtudiant().get(j).getNom()+" " +groupe.get(i).getTabEtudiant().get(j).getPrenom()+" ";
				}
				table.addCell(""+etudiant);	     
				c.insertSoutenance(projet,String.valueOf(groupe.get(i).getId()),heure+":"+minute,date);
				table.addCell(heure+":"+minute);
				min = min + Integer.parseInt(duree);
				minute = String.valueOf(min);
				if(min > 59){
					min = min % 60;
					h = h + 1;
					if(h<10){
						heure = "0"+String.valueOf(h);
					}else{
						heure = String.valueOf(h);
					}
					if(min<10){ 
						minute = "0"+String.valueOf(min);
					}else{
						minute = String.valueOf(min);
					}

				}
			}

			document.add(table);		
			document.close(); 
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
